////////////////////////////////////////////////////////////////////////////////
//
//  外部画像を1pxごとに表示
//
////////////////////////////////////////////////////////////////////////////////
package
{
	import flash.display.* ;
	import flash.events.* ;
	import flash.filters.ColorMatrixFilter;
	import flash.net.* ;
	import flash.system.* ;
	import flash.utils.getTimer ;
	
	[SWF(width="500", height="500", backgroundColor="0xFFFFFF", frameRate="30")]
	public class ParticleEffect extends Sprite
	{
		private var _loader:Loader ;
		private var _loaderInfo:LoaderInfo ;
		private var _particleList:Array = new Array( ) ;
		private var _bitmapList:ParticleBitmap ;
		private var _startTime:int ;
		
		public function ParticleEffect( )
		{
			init( ) ;
		}
		
		private function init( ):void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE ;
			stage.align = StageAlign.TOP_LEFT ;
			stage.quality = StageQuality.LOW ;
			
			Security.loadPolicyFile( "http://www.twist-cube.com/wonderfl/crossdomain.xml" ) ;
			
			var loaderContext:LoaderContext = new LoaderContext( ) ;
			loaderContext.checkPolicyFile = true ;
			
			_loader = new Loader( ) ;
			_loader.load( new URLRequest( "http://www.twist-cube.com/wonderfl/logo.png" ), loaderContext ) ;
			
			_loaderInfo = _loader.contentLoaderInfo ;
			_loaderInfo.addEventListener( Event.COMPLETE, onLoadComplete ) ;
		}
		
		private function onLoadComplete( event:Event ):void
		{
			_loaderInfo.removeEventListener( Event.COMPLETE, onLoadComplete ) ;
			_bitmapList = new ParticleBitmap( _loader ) ;
			
			var nStartX:Number = Math.floor( ( stage.stageWidth - _loader.width ) / 2 ) ;
			var nStartY:Number = Math.floor( ( stage.stageHeight - _loader.height ) / 2 ) ;
			
			/*
			_loader.x = nStartX ;
			_loader.y = nStartY ;
			
			addChild( _loader ) ;
			
			var grayScale:Array = 
				[
					1/3, 1/3, 1/3,   0, 0,
					1/3, 1/3, 1/3,   0, 0,
					1/3, 1/3, 1/3,   0, 0,
					  0,   0,   0, .05, 0
				] ;
			
			_loader.filters = [ new ColorMatrixFilter( grayScale ) ] ;
			*/
			
			var nLength:uint = _bitmapList.list.length ;
			var bmp:Bitmap ;
			var current:Object ;
			for ( var i:uint = 0; i < nLength; i++ )
			{
				current = _bitmapList.list[ i ] ;
				bmp = current.bitmap ;
				//bmp.alpha = 0 ;
				bmp.x = nStartX + current.x - 5000 * Math.random( ) + 2500 ;
				bmp.y = nStartY + current.y - stage.stageHeight - 5000 * Math.random( ) ;
				addChild( bmp ) ;
				_particleList.push( bmp ) ;
			}
			
			//_particleList.reverse( ) ;
			//_bitmapList.list.reverse( ) ;
			
			_startTime = getTimer( ) ;
			addEventListener( Event.ENTER_FRAME, onRender );
		}
		
		private function onRender( event:Event ):void
		{
			var nStartX:Number = Math.floor( ( stage.stageWidth - _loader.width ) / 2 ) ;
			var nStartY:Number = Math.floor( ( stage.stageHeight - _loader.height ) / 2 ) ;
			var nLength:uint = _particleList.length ;
			var bmp:Bitmap ;
			var current:Object ;
			var now:int = getTimer( ) ;
			var wait:Number ;
			for ( var i:uint = 0; i < nLength; i++ ) 
			{
				current = _bitmapList.list[ i ] ;
				
				// 下から吸着させる
				wait = ( 1 - ( current.y / _loader.height ) ) * 4000 ;
				
				if ( _startTime + wait > now ) continue ; 
				
				bmp = _particleList[ i ] ;
				bmp.x += ( ( nStartX + current.x ) - bmp.x ) * .2 ;
				bmp.y += ( ( nStartY + current.y ) - bmp.y ) * .2 ;
				//bmp.alpha += ( 1 - bmp.alpha ) * .1 ;
			}
		}
	}
}


import flash.display.* ;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Rectangle;

class ParticleBitmap
{
	private var _originImage:DisplayObject ;
	private var _list:Array = new Array( ) ;
	
	public function ParticleBitmap( _originImage:DisplayObject )
	{
		this._originImage = _originImage ;
		init( ) ;
	}
	
	private function init( ):void
	{
		var nW:Number = _originImage.width ;
		var nH:Number = _originImage.height 
		var originBitmapData:BitmapData = new BitmapData( _originImage.width, _originImage.height, true, 0x00FFFFFF ) ;
		originBitmapData.draw( _originImage ) ;
		
		var originBitmap:Bitmap = new Bitmap( originBitmapData ) ;
		
		for ( var i:uint = 0; i < nW; i++ )
		{
			for ( var j:int = 0; j < nH; j++ )
			{
				var color:uint = originBitmapData.getPixel32( i, j ) ;
				if ( color == 0 ) continue ;
				var bmpData:BitmapData = new BitmapData( 1, 1, true, color ) ;
				var bmp:Bitmap = new Bitmap( bmpData ) ;
				_list.push( { bitmap:bmp, x:i, y:j } ) ;
			}
		}
	}
	
	public function get list( ):Array
	{
		return _list ;
	}
	
	public function set list( _list:Array ):void
	{
		this._list = _list ;
	}
}